package com.xnyzc.lhy.mis.service.impl.coupon;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xnyzc.lhy.common.exception.CouponException;
import com.xnyzc.lhy.common.penum.coupon.CouponErrorCode;
import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.coupon.CouponGroupSearchResult;
import com.xnyzc.lhy.mis.entity.coupon.OaSysCouponGroup;
import com.xnyzc.lhy.mis.mapper.coupon.OaSysCouponGroupMapper;
import com.xnyzc.lhy.mis.service.coupon.IOaSysCouponGroupService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 优惠券组合 服务实现类
 * </p>
 *
 * @author haoyfa
 * @since 2019-12-27
 */
@Service
public class OaSysCouponGroupServiceImpl extends ServiceImpl<OaSysCouponGroupMapper, OaSysCouponGroup> implements IOaSysCouponGroupService {
    @Autowired
    OaSysCouponGroupMapper oaSysCouponGroupMapper;

    /**
     * 新增优惠券组
     *
     * @return
     */
    @Override
    public OaSysCouponGroup createCouponInfo(Long batchId, Long fileId) {
        //获取优惠券数据
        Date createDate = new Date();
        OaSysCouponGroup oaSysCouponGroup = new OaSysCouponGroup();
        oaSysCouponGroup.setBatchId(batchId);
        oaSysCouponGroup.setFileId(fileId);

        Long groupId = IDUtil.nextId();
        oaSysCouponGroup.setId(groupId);
        oaSysCouponGroup.setCreateTime(createDate);
        oaSysCouponGroup.setCreateUser(TokenUtil.getTokenUserId());
        oaSysCouponGroup.setDeleted(0);
        oaSysCouponGroup.setVersion(0L);
        oaSysCouponGroup.setUpdateTime(createDate);

        oaSysCouponGroupMapper.insert(oaSysCouponGroup);

        return oaSysCouponGroup;
    }

    /**
     * 获取优惠券分组记录（根据批次ID）
     *
     * @return
     */
    @Override
    public List<CouponGroupSearchResult> queryCouponGroupList(String batchId) {
        if(StringUtils.isEmpty(batchId))
        {
            throw new CouponException(CouponErrorCode.paramError);
        }

        //查询优惠券分组
        QueryWrapper<OaSysCouponGroup> oaSysCouponInfoWrapper = new QueryWrapper<OaSysCouponGroup>();

        //优惠券批次ID
        oaSysCouponInfoWrapper.eq(OaSysCouponGroup.BATCH_ID, batchId);

        //正常记录 =0
        oaSysCouponInfoWrapper.eq(OaSysCouponGroup.DELETED, 0);

        //查询数据
        List<OaSysCouponGroup> oaSysCouponGroupList = oaSysCouponGroupMapper.selectList(oaSysCouponInfoWrapper);

        return this.createCouponGroupSearchResult(oaSysCouponGroupList);
    }

    /**
     * 创建查询返回数据
     * @param oaSysCouponGroupList
     * @return
     */
    private List<CouponGroupSearchResult> createCouponGroupSearchResult(List<OaSysCouponGroup> oaSysCouponGroupList) {
        if(oaSysCouponGroupList !=null
                && oaSysCouponGroupList.size() >0){
            List<CouponGroupSearchResult> couponGroupSearchResultList = new ArrayList<CouponGroupSearchResult>(oaSysCouponGroupList.size());
            for (OaSysCouponGroup oaSysCouponGroup : oaSysCouponGroupList) {
                CouponGroupSearchResult couponGroupSearchResult = new CouponGroupSearchResult();

                //ID
                couponGroupSearchResult.setGroupId(oaSysCouponGroup.getId().toString());
                //分组文件ID
                couponGroupSearchResult.setFileId(oaSysCouponGroup.getFileId().toString());
                //发放时间
                couponGroupSearchResult.setPublishDate(DateUtil.convertDateToString(oaSysCouponGroup.getCreateTime()));

                couponGroupSearchResultList.add(couponGroupSearchResult);
            }

            return couponGroupSearchResultList;
        }
        else{
            return null;
        }
    }
}
